<?php

namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use api\modules\v1\forms\system\SystemConfigForm;
use common\helpers\CommonHelper;
use services\system\SystemConfigService;
use Yii;

/**
 * Class SystemConfigController
 * @package api\modules\v1\controllers
 */
class SystemConfigController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/system-config/wx-mini-info 微信首页配置信息
     * @apiName wx-mini-info
     * @apiGroup SystemConfig
     *
     *
     * @apiSuccess {Object} data                    返回内容
     * @apiSuccess {Array}  data.image_type         支持图片格式
     * @apiSuccess {Array}  data.video_type         支持视频格式
     */
    public function actionWxMiniInfo()
    {
        $service = new SystemConfigService();
        return $service->getWxMiniConfigInfo();
    }

    /**
     * @api {post} /v1/system-config/edit-wx-mini 编辑微信小程序配置
     * @apiName edit-wx-mini
     * @apiGroup SystemConfig
     *
     * @apiBody {String} c_home_page_title         标题
     * @apiBody {Number} c_home_page_banner_type   类型【1图片、2视频】
     * @apiBody {Array}  c_home_page_banner_image  图片【type=1必填 格式：【'1.jpg', '2.jpg'】 site: wx_mini module: index_img】
     * @apiBody {String} c_home_page_banner_video  视频【type=2必填 site: wx_mini module: index_video】
     * @apiBody {String} c_home_page_banner_content 内容
     *
     * @apiSuccess {Object} data        返回内容
     */
    public function actionEditWxMini()
    {
        $params = Yii::$app->request->post();
        $params['c_home_page_banner_image'] = $params['c_home_page_banner_image'] ?? [];
        $params['c_home_page_banner_video'] = $params['c_home_page_banner_video'] ?? '';

        $model = new SystemConfigForm();
        $model->setScenario('edit_wx_config');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new SystemConfigService();
        return $service->editWxMini($model);
    }
}
